package com.ruoyi.web.controller.life;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.life.domain.LifeDiary;
import com.ruoyi.life.service.ILifeDiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 日记Controller
 *
 * @author ruoyi
 * @date 2025-05-28
 */
@RestController
@RequestMapping("/life/diary")
public class LifeDiaryController extends BaseController {
    @Autowired
    private ILifeDiaryService lifeDiaryService;

    /**
     * 查询日记列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LifeDiary lifeDiary) {
        startPage();
        List<LifeDiary> list = lifeDiaryService.selectLifeDiaryList(lifeDiary);
        return getDataTable(list);
    }

    /**
     * 导出日记列表
     */
    @Log(title = "日记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LifeDiary lifeDiary) {
        List<LifeDiary> list = lifeDiaryService.selectLifeDiaryList(lifeDiary);
        ExcelUtil<LifeDiary> util = new ExcelUtil<LifeDiary>(LifeDiary.class);
        util.exportExcel(response, list, "日记数据");
    }

    /**
     * 获取日记详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(lifeDiaryService.selectLifeDiaryById(id));
    }

    /**
     * 新增日记
     */
    @Log(title = "日记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LifeDiary lifeDiary) {
        return toAjax(lifeDiaryService.insertLifeDiary(lifeDiary));
    }

    /**
     * 修改日记
     */
    @Log(title = "日记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LifeDiary lifeDiary) {
        return toAjax(lifeDiaryService.updateLifeDiary(lifeDiary));
    }

    /**
     * 删除日记
     */
    @Log(title = "日记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(lifeDiaryService.deleteLifeDiaryByIds(ids));
    }
}
